package com.example.customviewtest

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast

class CustomView(context : Context, attrs : AttributeSet) : View(context, attrs) {

    private val layoutInflater by lazy {
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    }
    private val mCustomView by lazy {
        layoutInflater.inflate(R.layout.custom_view, null) as LinearLayout
    }

    private val text by lazy {
        mCustomView.findViewById<TextView>(R.id.tv_text)
    }

    private val btn by lazy {
        mCustomView.findViewById<Button>(R.id.btn_close)
    }

    private val mCustomSubViews = ArrayList<View>()

    init {
//        isClickable = true
//        isLongClickable = true
//        setOnClickListener{
//            Toast.makeText(context, "Clicked CustomView", Toast.LENGTH_SHORT).show()
//        }
        mCustomSubViews.add(btn)
        mCustomSubViews.add(text)
        text.setOnClickListener {
            Toast.makeText(context, "Clicked Text", Toast.LENGTH_SHORT).show()
        }
        btn.setOnClickListener {
            Toast.makeText(context, "Clicked Btn", Toast.LENGTH_SHORT).show()
        }
//        mCustomView.setOnClickListener {
//            Toast.makeText(context, "Clicked mCustomView", Toast.LENGTH_SHORT).show()
//        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        mCustomView.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY))
        setMeasuredDimension(mCustomView.measuredWidth, mCustomView.measuredHeight)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawCustomView(canvas)
        val paint = Paint()
        paint.color = context.getColor(android.R.color.holo_red_dark)
        paint.textSize = 80f
        canvas.drawText("131321", 0f, 100f, paint)
    }

    private fun drawCustomView(canvas: Canvas) {
        // 获取 mCustomView 的测量宽高
        val measuredWidth = mCustomView.measuredWidth
        val measuredHeight = mCustomView.measuredHeight

        // 计算 mCustomView 在 CustomView 中的绘制位置
        val left = (width - measuredWidth) / 2
        val top = (height - measuredHeight) / 2
        val right = left + measuredWidth
        val bottom = top + measuredHeight

        // 设置绘制区域并绘制 mCustomView
        mCustomView.layout(left, top, right, bottom)
        mCustomView.draw(canvas)
    }

    private val TAG = "CustomView"

    override fun onTouchEvent(event: MotionEvent): Boolean {
        Log.i(TAG, "onTouchEvent: enter")
        if (event.action == MotionEvent.ACTION_UP) {
            if (event.x >= mCustomView.left && event.x <= mCustomView.right &&
                event.y >= mCustomView.top && event.y <= mCustomView.bottom
            ) {
                for (v in mCustomSubViews) {
                    if (event.x >= v.left && event.x <= v.right &&
                        event.y >= v.top && event.y <= v.bottom
                    ) {
                        v.performClick();
                    }
                }
                // 触发按钮的点击事件
                Log.i(TAG, "mCustomView.onTouchEvent: enter")
//            mCustomView.dispatchTouchEvent(event)
                return true
            }
        }
        return true || /*mCustomView.onTouchEvent(event) || */super.onTouchEvent(event)
    }
}